﻿namespace Vdc.Grid.Design
{
    using Vdc.Grid;
    using System;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Web.UI.Design;
    using System.Web.UI.Design.WebControls;

    [SupportsPreviewControl(true)]
    public sealed class GridDesigner : DataBoundControlDesigner
    {
        private DesignerActionListCollection _actionLists;
        private string BodyRegionPrefix;
        private Vdc.Grid.Grid GridComponent;
        private string HeaderRegionPrefix;
        internal bool[] RegionHighlights;

        private void AddGridTemplatesGroups(TemplateGroupCollection templateGroups)
        {
            Vdc.Grid.Grid component = (Vdc.Grid.Grid) base.Component;
            if (component.Templates.Count > 0)
            {
                TemplateGroup group = new TemplateGroup(component.ID + " Templates");
                foreach (GridTemplate template in component.Templates)
                {
                    TemplateDefinition templateDefinition = new TemplateDefinition(this, template.ID, template, "Template", false) {
                        SupportsDataBinding = true
                    };
                    group.AddTemplateDefinition(templateDefinition);
                }
                templateGroups.Add(group);
            }
        }

        private IDataSourceViewSchema GetDataSourceSchema()
        {
            DesignerDataSourceView designerView = base.DesignerView;
            if (designerView != null)
            {
                return designerView.Schema;
            }
            return null;
        }

        public override string GetDesignTimeHtml(DesignerRegionCollection designerRegions)
        {
            if (this.GridComponent.Columns.NeedIndexRegeneration())
            {
                this.GridComponent.GenerateColumnIndexes(true);
            }
            for (int i = 0; i < this.GridComponent.Columns.Count; i++)
            {
                if (this.GridComponent.Columns[i].Index >= 0)
                {
                    designerRegions.Add(new DesignerRegion(this, this.HeaderRegionPrefix + this.GridComponent.Columns[i].Index.ToString()));
                    if (this.RegionHighlights[i])
                    {
                        designerRegions[i].Highlight = true;
                    }
                }
            }
            for (int j = 0; j < this.GridComponent.Columns.Count; j++)
            {
                if (this.GridComponent.Columns[j].Index >= 0)
                {
                    designerRegions.Add(new DesignerRegion(this, this.BodyRegionPrefix + this.GridComponent.Columns[j].Index.ToString()));
                    if (this.RegionHighlights[j])
                    {
                        designerRegions[j + this.GridComponent.Columns.Count].Highlight = true;
                    }
                }
            }
            return base.GetDesignTimeHtml(designerRegions);
        }

        internal void HighlightColumn(int columnIndex)
        {
            for (int i = 0; i < this.GridComponent.Columns.Count; i++)
            {
                if (i == columnIndex)
                {
                    this.RegionHighlights[i] = true;
                    this.RegionHighlights[i + this.GridComponent.Columns.Count] = true;
                }
                else
                {
                    this.RegionHighlights[i] = false;
                    this.RegionHighlights[i + this.GridComponent.Columns.Count] = false;
                }
            }
        }

        public override void Initialize(IComponent component)
        {
            base.Initialize(component);
            base.SetViewFlags(ViewFlags.TemplateEditing, true);
            this.GridComponent = base.Component as Vdc.Grid.Grid;
            this.RegionHighlights = new bool[this.GridComponent.Columns.Count * 2];
            this.HeaderRegionPrefix = "ob_" + this.GridComponent.ID + "_Header_";
            this.BodyRegionPrefix = "ob_" + this.GridComponent.ID + "_Body_";
        }

        protected override void OnClick(DesignerRegionMouseEventArgs e)
        {
            base.OnClick(e);
            if (e.Region != null)
            {
                for (int i = 0; i < (this.RegionHighlights.Length / 2); i++)
                {
                    if (e.Region.Name == (this.HeaderRegionPrefix + this.GridComponent.Columns[i].Index.ToString()))
                    {
                        this.RegionHighlights[i] = true;
                        this.RegionHighlights[i + this.GridComponent.Columns.Count] = true;
                    }
                    else
                    {
                        this.RegionHighlights[i] = false;
                        this.RegionHighlights[i + this.GridComponent.Columns.Count] = false;
                    }
                }
                this.UpdateDesignTimeHtml();
            }
        }

        public override void UpdateDesignTimeHtml()
        {
            try
            {
                if (this.RegionHighlights.Length != (this.GridComponent.Columns.Count * 2))
                {
                    int num = this.GridComponent.Columns.Count * 2;
                    this.RegionHighlights = new bool[num];
                }
            }
            catch (Exception exception1)
            {
                if (exception1 != null)
                {
                }
            }
            base.UpdateDesignTimeHtml();
        }

        public override DesignerActionListCollection ActionLists
        {
            get
            {
                this._actionLists = new DesignerActionListCollection();
                this._actionLists.AddRange(base.ActionLists);
                int highlightedColumnIndex = -1;
                try
                {
                    for (int i = 0; i < this.GridComponent.Columns.Count; i++)
                    {
                        if ((this.GridComponent.Columns[i].Index >= 0) && this.RegionHighlights[this.GridComponent.Columns[i].Index])
                        {
                            highlightedColumnIndex = this.GridComponent.Columns[i].Index;
                            goto Label_0094;
                        }
                    }
                }
                catch (Exception exception1)
                {
                    if (exception1 != null)
                    {
                    }
                }
            Label_0094:
                this._actionLists.Add(new GridActionList(this, highlightedColumnIndex));
                return this._actionLists;
            }
        }

        public override TemplateGroupCollection TemplateGroups
        {
            get
            {
                TemplateGroupCollection templateGroups = base.TemplateGroups;
                this.AddGridTemplatesGroups(templateGroups);
                return templateGroups;
            }
        }
    }
}

